﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using TaobaoMatch;

namespace TaoWeb.Controllers
{
    public class ValuesController : ApiController
    {
        // GET api/values
        [HttpGet]
        public IHttpActionResult Get([FromUri]string start, [FromUri]string limit)
        {
            int li = Int32.Parse(limit);
            int st = Int32.Parse(start) - 1;

            var lstNew = AppConfig.DicProduct.Values.Where(x => x.CreateTime > DateTime.Now.Date);

            var lstRecom = lstNew.OrderByDescending(x => x.SaleAmount).Take(5).ToList();

            lstRecom.AddRange(lstNew.Skip(st * li).Take(li));

            return Ok(new ModelListResponse()
            {
                Status = ResponseStatus.OK(),
                Count = lstNew.Count(),
                ResponseList = lstRecom
            });
        }

        // GET api/values/5
        public Product Get(string id)
        {
            return AppConfig.DicProduct.FirstOrDefault(x => x.Key == id).Value;
        }

        // POST api/values
        public void Post([FromBody]string value)
        {
        }

        [HttpPost]
        public IHttpActionResult Search([FromBody]JObject value)
        {
            int limit = GetIntValue(value, "limit");
            int start = GetIntValue(value, "start");

            List<Product> lst = new List<Product>();
            int count = 0;

            switch (GetValue(value, "type"))
            {
                case "All":

                    string condition = GetValue(value, "condition");

                    lst = AppConfig.DicProduct.Values.Where(x => x.Title.Contains(condition) || x.Description.Contains(condition)).ToList();
                    count = lst.Count;
                    if (count > 0)
                        lst = lst.Skip(start * limit).Take(limit).ToList();

                    break;
                case "Cate":

                    lst = AppConfig.DicProduct.Values.Where(x => x.Cate == GetValue(value, "condition")).ToList();

                    count = lst.Count;
                    if (count > 0)
                        lst = lst.Skip(start * limit).Take(limit).ToList();

                    break;
                case "Index":

                    var lstNew = AppConfig.DicProduct.Values.Where(x => x.CreateTime > DateTime.Now.Date);

                    count = lstNew.Count();

                    if (count > 0)
                    {
                        lst = lstNew.OrderByDescending(x => x.SaleAmount).Take(5).ToList();

                        lst.AddRange(lstNew.Skip(start * limit).Take(limit));
                    }

                    break;
            }

            return Ok(new ModelListResponse()
            {
                Status = ResponseStatus.OK(),
                Count = count,
                ResponseList = lst
            });
        }

        // PUT api/values/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/values/5
        public void Delete(int id)
        {
        }

        #region helper

        private string GetValue(JObject value, Enum name)
        {
            if (value == null)
                return string.Empty;

            return value[name.ToString()] == null ? string.Empty : value[name.ToString()].ToString();
        }

        private string GetValue(JObject value, string name)
        {
            if (value == null)
                return string.Empty;

            return value[name] == null ? string.Empty : value[name].ToString();
        }

        private int GetIntValue(JObject value, Enum name)
        {
            if (value == null)
                return 0;

            return value[name.ToString()] == null ? 0 : value[name.ToString()].ToString().ToInt();
        }

        private int GetIntValue(JObject value, string name)
        {
            if (value == null)
                return 0;

            return value[name] == null ? 0 : value[name].ToString().ToInt();
        }

        #endregion
    }
}
